package com.gdut.simple.view.activity.user;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;

import com.gdut.simple.R;
import com.gdut.simple.contract.RegisterContract;
import com.gdut.simple.event.RxBus;
import com.gdut.simple.event.UserEvent;
import com.gdut.simple.presenter.user.RegisterPresenter;
import com.gdut.simple.view.basic.AbsBasicActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends AbsBasicActivity implements RegisterContract.View {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_reg_acc_text)
    EditText mRegAccEditText;
    @BindView(R.id.id_reg_psw_text)
    EditText mRegPswEditText;
    @BindView(R.id.id_reg_psw2_text)
    EditText mRegPswEditText2;

    @OnClick(R.id.id_reg_button)
    void register() {
        mRegisterPresenter.register();
    }

    private RegisterContract.Presenter mRegisterPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initToolbar();
        new RegisterPresenter(this);
    }

    private void initToolbar() {
        mToolbar.setTitle(R.string.user_register);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }


    @Override
    public String getAccount() {
        return mRegAccEditText != null ? mRegAccEditText.getText().toString().trim() : "";
    }

    @Override
    public String getPassword() {
        return mRegPswEditText != null ? mRegPswEditText.getText().toString().trim() : "";
    }

    @Override
    public String getPassword2() {
        return mRegPswEditText2 != null ? mRegPswEditText2.getText().toString().trim() : "";
    }

    @Override
    public void next() {
        RxBus.getDefault().post(new UserEvent(true));
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 100);
    }

    @Override
    public void setPresenter(@NonNull RegisterContract.Presenter presenter) {
        this.mRegisterPresenter = presenter;
    }
}
